package com.ss.android.newmedia.data;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class Banner implements Parcelable{
	public static String ACTION_API="api";
	public static String ACTION_API_LIST="api_list";
	public static String ACTION_DOWNLOAD="download";
	public static final Parcelable.Creator<Banner> CREATOR=new Parcelable.Creator<Banner>() {
		@Override
		public Banner createFromParcel(Parcel source) {
			return new Banner(source);
		}

		@Override
		public Banner[] newArray(int size) {
			return new Banner[size];
		}
	};
	public static final String JSON_ACTION="action";
	public static final String JSON_ACTION_URL="action_url";
	public static final String JSON_DATA="data";
	public static final String JSON_DESCRIPTION="description";
	public static final String JSON_DISPLAY="display";
	public static final String JSON_IMAGE_URL="image_url";
	public static final String JSON_NAME="name";
	public static final String JSON_PACKAGE="package";
	public static final String JSON_VERBOSE_NAME="verbose_name";
	public String action;
	public String[] actionUrls;
	public String description;
	public String display;
	public String imageUrl;
	public String name;
	public String packageName;
	public int resourceId=-1;
	public String verboseName;
	
	public Banner(){
		
	}
	
	private Banner(Parcel source){
		this.packageName=source.readString();
		this.name=source.readString();
		this.verboseName=source.readString();
		this.imageUrl=source.readString();
		source.readStringArray(this.actionUrls);
		this.description=source.readString();
		this.action=source.readString();
		this.display=source.readString();
		this.resourceId=source.readInt();
	}
	
	public static boolean decode(String str,List<Banner> list) throws Exception{
		boolean isSuccess=true;
		try{
			JSONArray dataArray=new JSONObject(str).getJSONArray("data");
			int i=dataArray.length();
			for(int j=0;j<i;j++){
				JSONObject jsonObj=dataArray.getJSONObject(j);
				Banner banner=new Banner();
				if(!jsonObj.isNull("name")){
					banner.name=jsonObj.getString("name");
				}
				if(!jsonObj.isNull("package")){
					banner.packageName=jsonObj.getString("package");
				}
				if(!jsonObj.isNull("verbose_name")){
					banner.verboseName=jsonObj.getString("verbose_name");
				}
				if(!jsonObj.isNull("image_url")){
					banner.imageUrl=jsonObj.getString("image_url");
				}
				boolean bool=jsonObj.isNull("action_url");
				if(!bool){
					try{
						JSONArray urlArray=jsonObj.getJSONArray("action_url");
						banner.actionUrls=new String[urlArray.length()];
						for(int k=0;i<urlArray.length();k++){
							banner.actionUrls[k]=urlArray.getString(k);
						}
					}catch(Exception e){
						banner.actionUrls=new String[1];
						banner.actionUrls[0]=jsonObj.getString("action_url");
					}
				}
				if(!jsonObj.isNull("description")){
					banner.description=jsonObj.getString("description");
				}
				if(!jsonObj.isNull("action")){
					banner.action=jsonObj.getString("action");
				}
				if(!jsonObj.isNull("display")){
					banner.display=jsonObj.getString("display");
				}
				list.add(banner);
			}
		}catch(JSONException e){
			e.printStackTrace();
			isSuccess=false;
		}
		return isSuccess;
	}
	
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(this.packageName);
		dest.writeString(this.name);
		dest.writeString(this.verboseName);
		dest.writeString(this.imageUrl);
		dest.writeStringArray(this.actionUrls);
		dest.writeString(this.description);
		dest.writeString(this.action);
		dest.writeString(this.display);
		dest.writeInt(this.resourceId);
	}
	
}
